package com.lancoo.znbkxx.teacher.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import com.lancoo.znbkxx.teacher.R;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.internal.InternalAbstract;

/**
 * @author : Admin
 * @date : 2019/10/24 19:17
 * @desc :
 */
public class MyfooterView extends InternalAbstract implements RefreshFooter {
    private TextView tvHint;
    private ImageView ivHeader;
    private ImageView ivShadow;
    private TextView tvNoMore;
    private ScaleAnimation scaleHeader;
    private ScaleAnimation scaleShadow;
    //    private static final String REFRESH_HEADER_PULLING = "上拉即可加载";
//    private static final String REFRESH_HEADER_LOADING = "加载中...";
//    private static final String REFRESH_HEADER_RELEASE = "释放即可加载";
//    private static final String REFRESH_HEADER_FINISH = "加载成功";
//    private static final String REFRESH_HEADER_FAILED = "加载失败";
    private static final String REFRESH_HEADER_PULLING = "";
    private static final String REFRESH_HEADER_LOADING = "";
    private static final String REFRESH_HEADER_RELEASE = "";
    private static final String REFRESH_HEADER_FINISH = "";
    private static final String REFRESH_HEADER_FAILED = "";

    public MyfooterView(Context context) {
        this(context, null);
    }

    public MyfooterView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyfooterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.view_header, this);
        tvHint = view.findViewById(R.id.tv_hint);
        ivHeader = view.findViewById(R.id.iv_header);
        ivShadow = view.findViewById(R.id.iv_shadow);
        tvNoMore = view.findViewById(R.id.tv_no_more);
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        ivHeader.clearAnimation();
        ivShadow.clearAnimation();
        if (success) {
            tvHint.setText(REFRESH_HEADER_FINISH);
        } else {
            tvHint.setText(REFRESH_HEADER_FAILED);
        }
        super.onFinish(refreshLayout, success);
        return 500;//延迟500毫秒之后再弹回
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case PullUpToLoad://下拉过程
                tvHint.setText(REFRESH_HEADER_RELEASE);
                executeAnimation();
                break;
            case ReleaseToLoad://松开刷新
                tvHint.setText(REFRESH_HEADER_PULLING);
                break;
            case Loading://loading中
                tvHint.setText(REFRESH_HEADER_LOADING);
        }
    }

    private void executeAnimation() {
        scaleHeader = new ScaleAnimation(0.8f, 1.2f, 0.8f, 1.2f, Animation.RELATIVE_TO_SELF, 0.8f, Animation.RELATIVE_TO_SELF, 0.8f);
        scaleHeader.setDuration(1000);
        scaleHeader.setRepeatCount(Animation.INFINITE);
        scaleHeader.setRepeatMode(Animation.REVERSE);
        scaleShadow = new ScaleAnimation(0.8f, 1.2f, 0.8f, 1.2f, Animation.RELATIVE_TO_SELF, 0.8f, Animation.RELATIVE_TO_SELF, 0.8f);
        scaleShadow.setDuration(1000);
        scaleShadow.setRepeatCount(Animation.INFINITE);
        scaleShadow.setRepeatMode(Animation.REVERSE);
        ivHeader.startAnimation(scaleHeader);
        ivShadow.startAnimation(scaleShadow);
    }

    @Override
    public boolean setNoMoreData(boolean noMoreData) {
        if (noMoreData) {
            tvNoMore.setVisibility(View.VISIBLE);
        } else {
            tvNoMore.setVisibility(View.GONE);
        }
        return noMoreData;
    }
}
